package com.itheima.reggie.web;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.pojo.Category;
import com.itheima.reggie.pojo.Dish;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.ICategoryService;
import com.itheima.reggie.service.IDishService;
import com.itheima.reggie.service.ISetmealDishService;
import com.itheima.reggie.service.ISetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private ISetmealDishService setmealDishService;

    @Autowired
    private ISetmealService setmealService;

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    IDishService dishService;

    private R<String> save(@RequestBody SetmealDto setmealDto){

        setmealService.saveWithDish(setmealDto);

        return R.success("添加套餐成功");
    }

    /**
     * 套餐分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(Integer page,Integer pageSize,String name){

        //分页构造器对象
        Page<Setmeal> pageInfo = new Page<>(page, pageSize);
        Page<SetmealDto> dtoPage = new Page<>();

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();

        //条件查询，根据name进行模糊查询
        queryWrapper.like(name != null,Setmeal::getName,name);

        //排序查询，根据更新时间
        queryWrapper.orderByAsc(Setmeal::getUpdateTime);

        setmealService.page(pageInfo,queryWrapper);

        //拷贝对象
        BeanUtils.copyProperties(pageInfo,dtoPage,"records");
        List<Setmeal> records = pageInfo.getRecords();

        List<SetmealDto> list = records.stream().map((item) -> {
            SetmealDto setmealDto = new SetmealDto();
            //对象拷贝
            BeanUtils.copyProperties(item,setmealDto);
            //分类id
            Long categoryId = item.getCategoryId();
            //根据分类id查询分类对象
            Category category = categoryService.getById(categoryId);
            if(category != null){
                //分类名称
                String categoryName = category.getName();
                setmealDto.setCategoryName(categoryName);
            }
            return setmealDto;
        }).collect(Collectors.toList());


        return R.success(pageInfo);
    }

    /**
     * 删除套餐
     * @param
     * @return
     */
    @DeleteMapping
    @CacheEvict(value = "seta",allEntries = true)
    public R<String> delete(@RequestParam List<Long> ids) {
        log.info("收到删除套餐请求{}", ids.toString());
        if (setmealService.removeWithDish(ids)) {
            return R.success("套餐数据删除成功~");
        } else {
            return R.error("套餐数据删除失败！");
        }
    }

    /**
     * 停售/起售修改
     *
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(@PathVariable Integer status, @RequestParam List<Long> ids) {
        log.info("收到停售/起售请求：" + ids.toString());
        LambdaUpdateWrapper<Setmeal> lqw = new LambdaUpdateWrapper<>();
        lqw.set(Setmeal::getStatus, status);
        lqw.in(Setmeal::getId, ids);
        boolean b = setmealService.update(lqw);
        if (b) {
            return R.success("状态修改成功");
        } else {
            return R.error("状态修改失败");
        }
    }

    /**
     * 修改套餐前的套餐数据回显,未完成
     * @param
     * @return
     */
    @GetMapping("/dish/{id}")
    public R<SetmealDto> getById(@PathVariable Long id) {
        log.info("前台套餐点击查看具体菜品请求...{}", id);
        Setmeal setMeal = setmealService.getById(id);
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setImage(setMeal.getImage());
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId, id);
        List<SetmealDish> list = setmealDishService.list(queryWrapper);
        setmealDto.setSetmealDishes(list);
        return R.success(setmealDto);
    }

    /**
     * 修改套餐数据
     *
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody SetmealDto setmealDto) {
        log.info("收到修改套餐数据:{}", setmealDto.toString());
        if (setmealService.updateWithDish(setmealDto)) {
            return R.success("套餐修改成功~");
        } else {
            return R.error("套餐修改失败！");
        }
    }
}
